﻿using System;
using System.Drawing;
using System.Windows.Forms;
using PushpinGeneratorDemo.Properties;
using SimplePushpinGenerator;

namespace PushpinGeneratorDemo
{
    public partial class Form1 : Form
    {
        private readonly BalloonCaptionPainter _caption = new BalloonCaptionPainter();
        private readonly BalloonCaptionPainter _captionPainterForPin = new BalloonCaptionPainter();
        private Image _currentCaptionImage;
        private Image _currentPinImage;
        private readonly PushPinPainter _pin = new PushPinPainter();


        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            //Pin painter setup
            propPin.SelectedObject = _pin;
            _captionPainterForPin.PointerHeightPercent = 0;

            tbPinBuffer.Value = _pin.PinHeadBufferPercent;
            tbPinLength.Value = _pin.PinLength;

            chkPinImgOutside.Checked = _pin.ImageOutsideReflection;

            RefreshPin();

            //Caption painter setup
            propCaption.SelectedObject = _caption;
            tbPointerHeight.Value = _caption.PointerHeightPercent;
            tbPointerWidth.Value = _caption.PointerWidthPercent;
            
            RefreshCaption();
        }

        private void RefreshPin()
        {
            Image img;
            if (txtPinLabel.Visible)
            {
                Image caption = _captionPainterForPin.CreateImage(txtPinLabel.Text, _currentPinImage);
                img = _pin.CreateImage(caption);
            }
            else
            {
                img = _pin.CreateImage(_currentPinImage);
            }

            img = img.Rotate(tbPinRotation.Value);

            pbPinImage.Image = img;
        }

        private void RefreshCaption()
        {
            Image img = _caption.CreateImage(txtCaptionText.Text, _currentCaptionImage);
            if (tbCaptionRotation.Value != 0)
                img = img.Rotate(tbCaptionRotation.Value);

            pbCaptionImage.Image = img;
        }


        private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            RefreshPin();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            _currentPinImage = toolStripButton1.Image;
            RefreshPin();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            _currentPinImage = toolStripButton2.Image;
            RefreshPin();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            _currentPinImage = toolStripButton3.Image;
            RefreshPin();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            _currentPinImage = null;
            RefreshPin();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            txtPinLabel.Visible = !txtPinLabel.Visible;
            RefreshPin();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            RefreshPin();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            _pin.PinHeadBufferPercent = tbPinBuffer.Value;
            RefreshPin();
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            _pin.PinLength = tbPinLength.Value;
            RefreshPin();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            _pin.ImageOutsideReflection = chkPinImgOutside.Checked;
            RefreshPin();
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            pbPinImage.BackgroundImage = chkPinSimulateMap.Checked
                                             ? Resources.doh
                                             : null;
        }

        private void txtCaptionText_TextChanged(object sender, EventArgs e)
        {
            RefreshCaption();
        }

        private void tbCaptionRotation_Scroll(object sender, EventArgs e)
        {
            RefreshCaption();
        }

        private void chkCaptionSimMap_CheckedChanged(object sender, EventArgs e)
        {
            pbCaptionImage.BackgroundImage = chkCaptionSimMap.Checked ? Resources.doh : null;
        }

        private void propCaption_Click(object sender, EventArgs e)
        {
        }

        private void propCaption_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            RefreshCaption();
        }

        private void btnSmallImg_Click(object sender, EventArgs e)
        {
            _currentCaptionImage = btnSmallImg.Image;
            RefreshCaption();
        }

        private void btnMedImg_Click(object sender, EventArgs e)
        {
            _currentCaptionImage = btnMedImg.Image;
            RefreshCaption();
        }

        private void btnLargeImage_Click(object sender, EventArgs e)
        {
            _currentCaptionImage = btnLargeImage.Image;
            RefreshCaption();
        }

        private void btnToggleImg_Click(object sender, EventArgs e)
        {
            _currentCaptionImage = null;
            RefreshCaption();
        }

        private void btnTogglePin_Click(object sender, EventArgs e)
        {
            _currentCaptionImage = pbPinImage.Image;
            RefreshCaption();
        }

        private void tbPointerHeight_Scroll(object sender, EventArgs e)
        {
            _caption.PointerHeightPercent = tbPointerHeight.Value;
            RefreshCaption();
        }

        private void tbPointerWidth_Scroll(object sender, EventArgs e)
        {
            _caption.PointerWidthPercent = tbPointerWidth.Value;
            RefreshCaption();
        }
    }
}